package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.ExtractMoneyDetails;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.DateUtils;
import com.chain.BrotherExchange.viewmodel.AssetsDetailsViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import butterknife.Bind;

/**
 * 提币明细
 */
public class ExtractMoneyDetailsActivity extends BaseActivity {

    @Bind(R.id.state_image) ImageView stateImage;
    @Bind(R.id.state_text) TextView stateText;
    @Bind(R.id.order_date_text) TextView orderDateText;
    @Bind(R.id.coin_type_text) TextView coinTypeText;
    @Bind(R.id.extract_money_number) TextView extractMoneyNumber;
    @Bind(R.id.gas_text) TextView gasText;
    @Bind(R.id.account_number_text) TextView accountNumberText;
    @Bind(R.id.address_text) TextView addressText;
    @Bind(R.id.remark_text) TextView remarkText;
    @Bind(R.id.apply_button) Button applyButton;
    @Bind(R.id.cancel_order_layout) View mCancelOrderLayout;

    private AssetsDetailsViewModel mViewModel;

    public static void launch(Fragment fragment, ExtractMoneyDetails.DataBean.ListBean.ItemsBean item, int position) {
        Activity activity = (Activity) fragment.getContext();
        fragment.startActivityForResult(new Intent(activity, ExtractMoneyDetailsActivity.class)
                .putExtra(C.EXTRA_EXTRACT_MONEY_ORDER, item)
                .putExtra(C.EXTRA_POSITION, position), C.REQ_CANCEL_ORDER);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_extract_money_details;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_extract_money_details);
        ExtractMoneyDetails.DataBean.ListBean.ItemsBean itemsBean =
                (ExtractMoneyDetails.DataBean.ListBean.ItemsBean) getIntent()
                        .getSerializableExtra(C.EXTRA_EXTRACT_MONEY_ORDER);

        mViewModel = ViewModelProviders.of(this).get(AssetsDetailsViewModel.class);

        stateImage.setImageResource(itemsBean.getStateDrawable());
        stateText.setText(itemsBean.getStateText());
        orderDateText.setText(DateUtils.timestampToTime(itemsBean.getCreate_time()));
        coinTypeText.setText(itemsBean.getToken_name());
        extractMoneyNumber.setText(itemsBean.getAmount());
        gasText.setText(itemsBean.getFee());
        addressText.setText(itemsBean.getAddress());
        remarkText.setText(itemsBean.getRemarks());
        accountNumberText.setText(itemsBean.accountNumber());

        mCancelOrderLayout.setVisibility(itemsBean.isCancel() ? View.VISIBLE : View.GONE);

        applyButton.setOnClickListener(v -> cancelOrder(itemsBean.getId()));
    }

    private void cancelOrder(int id) {
        mViewModel.cancelExtractMoney(id).observe(this, resource -> {
            Androids.makeText(resource.message);
            if (resource.state == Resource.Status.SUCCESS) {
                finish(RESULT_OK, new Intent().putExtra(C.EXTRA_POSITION,
                        getIntent().getIntExtra(C.EXTRA_POSITION, -1)));
            }
        });
    }

}
